<script setup>
import {ref} from 'vue'
import {loginUser} from '@/api/user/user.js'
import { ElMessage } from 'element-plus'
import router from '@/router'
import {User,Lock,View} from "@element-plus/icons-vue";

const remember = ref(false)
// 定义表单
const loginForm=ref({
  username:'',
  password:''
})

// 校验规则
const loginRules = ref({
  username: [
    { required: true, message: '账号不能为空！', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在3-20位之间！', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空！', trigger: 'blur' },
    { min: 3, max: 60, message: '密码长度在3-20位之间！', trigger: 'blur' },
  ]
})

// 关联表单
const loginFormRef=ref(null)

// 登录方法
const submitForm=async()=>{
  if (!loginFormRef) return
  await loginFormRef.value.validate(async(valid, fields) => {
    if (valid) {
      let result = await loginUser(loginForm.value)
      if(result.code==200){
        //将用户信息存入到localStore
        localStorage.setItem('userInfo',JSON.stringify(result.data))
        //跳转至主页,路由
        ElMessage.success('登录成功！')
        router.push('/')
      }else{
        ElMessage.error('登录失败！')
      }
    } else {
      console.log('校验失败！!', fields)
    }
  })
}
</script>
<template>
  <el-container>
    <el-aside>
    </el-aside>
    <el-main>
      <div class="login-container">
        <h1 class="welcome-title">欢迎来到ModernWMS!👋🏻</h1>
        <el-form
            ref="loginFormRef"
            class="login-form"
            :model="loginForm"
            :rules="loginRules"
            label-width="auto"
        >
          <el-form-item  prop="username">
            <el-input
                :prefix-icon="User"
                v-model="loginForm.username"
                placeholder="请输入账号"
                size="large"
                clearable
            />
          </el-form-item>
          <el-form-item  prop="password">
            <el-input
                :prefix-icon="Lock"
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                size="large"
                show-password
            />
          </el-form-item>
          <el-checkbox v-model="remember" size="large"><span>记住账号密码</span></el-checkbox>
          <el-form-item>
            <el-button
                type="primary"
                @click="submitForm()"
                size="large"
                class="login-button"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
  </el-container>
</template>
<style scoped>
.el-aside {
  width: 1340px;
  height: 1040px;
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-image: url('@/assets/loginbackground.png');
}
.el-container {
  width: 100%;
  height: 100vh;
  min-height: 1040px;
}
.el-main {
  position: absolute;
  right: 0;
  width: 570px;
  height: 100%;
  min-height: 1040px;
  background-color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
}

.welcome-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}

.login-form {
  width: 100%;
}

.login-form :deep(.el-form-item__label) {
  font-weight: bold;
  color: #333;
}

.login-button {
  width: 500px;
  height: 45px;
  margin-top: 50px;
  background-color: #9c27b0;
}

.login-button:hover {
  background-color: #b863c6;
}

.login-button:active {
  background-color: #b863c6;
}
.el-checkbox__label span {
  color: #b2b0b5;
  font-size:16px ;
}

</style>

